#include "md_atv.h"
#include "ui_md_atv.h"



Md_atv::Md_atv(TCL_MSTAR_ATV_TV *mstar_atv,QList<uint> *ms_atv_num,QMap<uint,uchar>*ms_dtv_air_num,QMap<uint,uchar>* ms_dtv_cable_num,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Md_atv)
{
    ui->setupUi(this);
    Q_CHECK_PTR(mstar_atv);
    ms_atv=mstar_atv;
    dtv_air_num=ms_dtv_air_num;
    dtv_cable_num=ms_dtv_cable_num;
    atv_num=ms_atv_num;
    mstar_atv->Get_Data(&ch_data);
    solution=1;
    My_Init();
}

Md_atv::Md_atv(TCL_MSTAR_ATV_TV *mstar_atv, QList<uint> *tv_num, QWidget *parent):
    QWidget(parent),
    ui(new Ui::Md_atv)
{
    ui->setupUi(this);
    Q_CHECK_PTR(mstar_atv);
    ms_atv=mstar_atv;

    atv_num=tv_num;
    dtv_air_num=NULL;
    dtv_cable_num=NULL;
    mstar_atv->Get_Data(&ch_data);
    solution=0;
    My_Init();
}

Md_atv::Md_atv(TCL_MTK_ATV_TV *mtk_atv, QList<uint> *tv_num, QWidget *parent):
    QWidget(parent),
    ui(new Ui::Md_atv)
{
    ui->setupUi(this);
    atv_num=tv_num;
    Q_CHECK_PTR(mtk_atv);
    mt_atv=mtk_atv;
    dtv_air_num=NULL;
    dtv_cable_num=NULL;
    mtk_atv->Get_Data(&ch_data);
    solution=0;
    My_Init();
}

Md_atv::Md_atv(TCL_MTK_ATV_TV *mtk_atv,QList<uint> *mt_atv_num,QMap<uint,uchar> *mt_dtv_air_num,QMap<uint,uchar> *mt_dtv_cable_num,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Md_atv)
{
    ui->setupUi(this);
    mt_atv=mtk_atv;
    atv_num=mt_atv_num;
    dtv_air_num=mt_dtv_air_num;
    dtv_cable_num=mt_dtv_cable_num;
    Q_CHECK_PTR(mtk_atv);
    mtk_atv->Get_Data(&ch_data);
    solution=0;
    My_Init();

}
Md_atv::~Md_atv()
{
    delete ui;
    //delete ms_atv;
    //ms_atv=NULL;
   // delete mt_atv;
   // mt_atv=NULL;
   // atv_num=NULL;
   // dtv_air_num=NULL;
   // dtv_cable_num=NULL;
}

bool Md_atv::Is_Close()
{
    if(is_close)
        return true;
    else
        return false;
}


void Md_atv::My_Init()
{
    ui->msg->setStyleSheet("color:red");
    this->setStyleSheet("background-color:rgb(135,206,235)");
    ui->Item1->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item2->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item3->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item4->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item5->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item6->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->pB_Modify->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    connect(ui->pB_Modify,SIGNAL(clicked()),this,SLOT(Confirm()));
    if(0==solution)
    connect(ui->Item1,SIGNAL(editingFinished()),this,SLOT(Check_Num()));
    ui->label->setText("Channel Number:");
    ui->Item1->setText(ch_data.at(0));
    if(1==solution)
    {
        ui->Item1->setEnabled(false);
    }else
    {
        ui->Item1->setEnabled(true);
    }
   // connect(ui->Item1,SIGNAL(editingFinished()),this,SLOT(Check_Num()));
    connect(ui->Item1,SIGNAL(textChanged(QString)),this,SLOT(Set_Num(QString)));

    ui->label_2->setText("Channel Name:");
    ui->Item2->setText(ch_data.at(1));
    connect(ui->Item2,SIGNAL(textChanged(QString)),this,SLOT(Set_Name(QString)));
    ui->Item3->setEnabled(false);
    ui->label_3->setText("Channel　Class:");
    ui->Item3->setText(ch_data.at(2));
    QString tmp1="air";
    QString tmp2="cable";
    if(0==ch_data.at(2).compare(tmp1))
        ch_class=0;
    else if(0==ch_data.at(2).compare(tmp2))
        ch_class=1;
    else
        ch_class=2;
    connect(ui->Item4,SIGNAL(editingFinished()),this,SLOT(Check_Freq()));
    ui->Item4->setText(ch_data.at(3));
    connect(ui->Item4,SIGNAL(textChanged(QString)),this,SLOT(Set_Freq(QString)));
    ui->label_4->setText("Frequency(KHZ):");
    //ui->Item4->setEnabled(false);

    ui->label_5->setText("Farvorite Flag:");
    QStringList list=QStringList()
            <<"0"
            <<"1";
    ui->Item5->addItems(list);
    tmp1="0";
    tmp2="1";
    if(0==ch_data.at(4).compare(tmp1))
        ui->Item5->setCurrentIndex(0);
    else
        ui->Item5->setCurrentIndex(1);
    connect(ui->Item5,SIGNAL(activated(int)),this,SLOT(Set_Fav_Index(int)));

    ui->label_6->setText("Skip Flag:");
    ui->Item6->addItems(list);
    if(0==ch_data.at(5).compare(tmp1))
        ui->Item6->setCurrentIndex(0);
    else
        ui->Item6->setCurrentIndex(1);
    connect(ui->Item6,SIGNAL(activated(int)),this,SLOT(Set_Skip_Index(int)));

     is_close=false;
     ch_val=ch_data.at(0).toUInt();
    // connect(this,SIGNAL(Close()),this,SLOT(Set_Close()));
}


//void Md_atv::Close(TCL_MTK_ATV_TV * mt_atv,TCL_MSTAR_ATV_TV *ms_atv)
//{

//}

void Md_atv::Set_Freq(QString tmp)
{
    md_item.append(3);
}

void Md_atv::Set_Num(QString tmp)
{
    md_item.append(0);
}
void Md_atv::Set_Name(QString name)
{
    md_item.append(1);
}

void Md_atv::Set_Fav_Index(int index)
{
    md_item.append(4);
    ui->Item5->setCurrentIndex(index);

}

void Md_atv::Set_Skip_Index(int index)
{
    md_item.append(5);
    ui->Item6->setCurrentIndex(index);
}

void Md_atv::Confirm()
{
    QString tmp;
    ch_data.clear();
    ch_data.append(ui->Item1->text());
    ch_data.append(ui->Item2->text());
    QString tmp1="air";
    QString tmp2="cable";
    if(0==ui->Item3->text().compare(tmp1))
        tmp.setNum(0);
    else if(0==ui->Item3->text().compare(tmp2))
        tmp.setNum(1);
    else
        tmp.setNum(0);//容错
    ch_data.append(tmp);

    ch_data.append(ui->Item4->text());

    if(ui->Item5->currentIndex()==0)
        tmp.setNum(0);
    else if(ui->Item5->currentIndex()==1)
        tmp.setNum(1);
    else
        tmp.setNum(0);
    ch_data.append(tmp);

    if(ui->Item6->currentIndex()==0)
        tmp.setNum(0);
    else if(1==ui->Item6->currentIndex())
        tmp.setNum(1);
    else
        tmp.setNum(0);//error show the first data
    ch_data.append(tmp);
    ui->msg->setText("success");
    if(1==solution)
    {
        ms_atv->Set_Data(ch_data);
        emit Mstar_Close(*ms_atv,md_item);

    }
    else if(0==solution)
    {
        mt_atv->Set_Data(ch_data);
        emit Mtk_Close(*mt_atv,md_item);
    }else{
    }
    this->close();


}

void Md_atv::Check_Freq()
{
    QString text=ui->Item4->text();
    bool Flag=false;
    bool *ok=&Flag;
    text.toInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("The Freqency Error");
        ui->pB_Modify->setEnabled(false);
        return ;
    }else{
        ui->msg->setText("");
        ui->pB_Modify->setEnabled(true);
    }
}



void Md_atv::Check_Num()
{
    ui->msg->clear();
    bool flag=false;
    bool *ok=&flag;
    uint num=ui->Item1->text().toUInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("Channel Number Error");
        ui->pB_Modify->setEnabled(false);
        return ;
    }else{
        ui->pB_Modify->setEnabled(true);
    }
    CHECK_INPUT_EMPTY(ui->Item1->text());
    //Q_ASSERT(*ok==true);
    if(!atv_num||atv_num->isEmpty())
    {
        QMessageBox::information(this,"error","The atv num QList is empty");
        ui->msg->setText("The atv num QList is empty");
        ui->pB_Modify->setEnabled(false);
        //QTest:qSleep(100);
        //ui->msg->setText("");
       return ;
    }else
    {
        ui->msg->setText("");
        ui->pB_Modify->setEnabled(true);
    }
    ui->msg->setText("");
    for(int i=0;i<atv_num->size();i++)
    {
        if(num==atv_num->at(i)&&num!=ch_val)
        {
           //QMessageBox::information(this,"Error","The channel Number is repeated with other atv channel");
           ui->msg->setText("The Channel Number is repeated with other atv channels");
           //QTest::qSleep(1);
            ui->Item1->setText(ch_data.at(0));
            ui->pB_Modify->setEnabled(false);
            //ui->msg->setText("");
            return  ;
        }
    }
    if(dtv_air_num)
    {
        QMap<uint,uchar>::const_iterator i;
        for(i=dtv_air_num->constBegin();i!=dtv_air_num->constEnd();i++)
        {
            if(num==i.key())
            {
                //QMessageBox::warning(this,"Warning","The Channel Number is repeated with other dtv channels",QMessageBox::Ok);
                ui->msg->setText("The Channel Number is repeated with other dtv channels");
                //QTest::qSleep(1);
                ui->pB_Modify->setEnabled(false);
                ui->Item1->setText(ch_data.at(0));
                //ui->msg->setText("");
                return ;
            }
        }
    }
    //Update QList<uint>  atv_num
    for(int i=0;i<atv_num->size();i++)
    {
        if(atv_num->at(i)==ch_val)
        {
            atv_num->removeAt(i);
            atv_num->insert(i,num);
            break;
        }
    }
ui->pB_Modify->setEnabled(true);
}
